import { BaseService } from '@/core/base.service';
import { Process, Processor } from '@nestjs/bull';
import { Job } from 'bull';
import dayjs from 'dayjs';

@Processor('notice')
export class NoticeProcessor extends BaseService {
  @Process('publish')
  async publish(job: Job<{ id: number }>) {
    const { id } = job.data;

    await this.log.info(
      `公告自动发布: ${id} - ${dayjs().format('YYYY-MM-DD HH:mm:ss')}`,
    );

    const notice = await this.prisma.notice.findFirst({
      where: {
        id,
        isDeleted: false,
        isEnabled: false,
        isAutoPublish: true,
      },
    });
    if (!notice) return;
    if (!notice.jobId || notice.jobId !== job.id) return;

    await this.prisma.notice.update({
      where: {
        id,
      },
      data: {
        isEnabled: true,
      },
    });
  }
}
